package com.dqp.cslggroup.Api;

import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.ConnectSever.SeverIP;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.Result;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.Util.UserAgent;
import okhttp3.*;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 董琴平
 * @version 1.0
 * @date 2021/9/1 14:07
 */
public class xxmhApi {

    static String ip = SharedPreferencesUtil.getIp(MyApplication.getContext());

    /*
     * @Description: 信息门户登录
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/4
     */
    public static JSONObject login(String sno, String pwd, String remberMe) {
        if (ip == null) ip = SeverIP.getDefvalueUrl();
        if (StringUtil.isEmpty(sno, pwd)) return Result.error("参数不可为空");
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .build();
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("sno", sno)
                .addFormDataPart("password", pwd)
                .addFormDataPart("remberMe", remberMe)
                .build();
        Request request = new Request.Builder()
                .url(ip + "/cslg/xxmh/login")
                .method("POST", body)
                .addHeader("User-Agent", UserAgent.userAgent)
                .build();
        try {
            Response response = client.newCall(request).execute();
            if (response.body() == null) return Result.error("超时未响应！");
            String json = response.body().string();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("异常");
        }
    }

    /*
     * @Description: 消费信息
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/4
     */
    public static JSONObject getConsumerList(String sno, int currentPage, int pageNumber, Map<String, String> cookies) {
        if (ip == null) ip = SeverIP.getDefvalueUrl();
        try {
            if (StringUtil.isEmpty(sno, 9) || cookies == null) return Result.error("参数不合法");
            org.jsoup.Connection.Response response = Jsoup.connect(ip + "/cslg/xxmh/getConsumerList")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .data("sno", sno)
                    .data("currentPage", currentPage + "")
                    .data("pageNumber", pageNumber + "")
                    .cookies(cookies)
                    .timeout(60000)
                    .followRedirects(true)
                    .execute();
            if (response.statusCode() != 200) return Result.error(response.statusCode() + "");
            if (response.body() == null) return Result.error("响应超时！");
            String json = response.body();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSONObject.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("登录异常");
        }
    }

    /*
     * @Description: 借阅详情
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/4
     */
    public static JSONObject getBookDetail(String sno, int currentPage, int pageNumber, Map<String, String> cookies) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            if (StringUtil.isEmpty(sno, 9) || cookies == null) return Result.error("参数不合法");
            org.jsoup.Connection.Response response = Jsoup.connect(ip + "/cslg/xxmh/getBookDetail")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .data("sno", sno)
                    .data("currentPage", currentPage + "")
                    .data("pageNumber", pageNumber + "")
                    .cookies(cookies)
                    .timeout(60000)
                    .followRedirects(true)
                    .execute();
            if (response.statusCode() != 200) return Result.error(response.statusCode() + "");
            if (response.body() == null) return Result.error("响应超时！");
            String json = response.body();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSONObject.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("登录异常");
        }
    }

    /*
     * @Description: 获取图书借阅时间
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/4
     */
    public static JSONObject getBook(String sno, Map<String, String> cookies) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            if (StringUtil.isEmpty(sno, 9) || cookies == null) return Result.error("参数不合法");
            org.jsoup.Connection.Response response = Jsoup.connect(ip + "/cslg/xxmh/getBookBorrow")
                    .ignoreContentType(true)
                    .data("sno", sno)
                    .method(Connection.Method.GET)
                    .cookies(cookies)
                    .timeout(60000)
                    .followRedirects(true)
                    .execute();
            if (response.statusCode() != 200) return Result.error(response.statusCode() + "");
            if (response.body() == null) return Result.error("响应超时！");
            String json = response.body();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSONObject.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("登录异常");
        }
    }

    public static JSONObject jwxt(Map<String, String> cookies) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            if (cookies == null) return Result.error("参数不合法");
            org.jsoup.Connection.Response response = Jsoup.connect(ip + "/cslg/xxmh/jwxt")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .cookies(cookies)
                    .userAgent(UserAgent.userAgent)
                    .timeout(60000)
                    .followRedirects(true)
                    .execute();
            if (response.statusCode() != 200) return Result.error(response.statusCode() + "");
            if (response.body() == null) return Result.error("响应超时！");
            String json = response.body();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSONObject.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("异常");
        }
    }


}
